package com.cc.jdkjucdemo.CompletableFuture;

import java.util.concurrent.*;

public class CompletableFutureDemo01_02 {

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,
            50,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程start ......");
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("子线程执行了：" + Thread.currentThread().getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 10;
        }, executor);

        CompletableFuture<Object> objectCompletableFuture = integerCompletableFuture
                .thenCompose(integer -> CompletableFuture.supplyAsync(() -> {
                    System.out.println("thenCompose执行了：" + Thread.currentThread().getName());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("supplyAsync执行了：" + Thread.currentThread().getName());
                    return integer + 10;
                }));
        Object o = objectCompletableFuture.get();
        System.out.println("result = " + o);

        System.out.println("主线程end ......");
        executor.shutdown();
    }
}